/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.gui;

import it.polimi.se2.meteocal.control.CalendarManager;
import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.entity.Calendar;
import it.polimi.se2.meteocal.entity.Users;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.MenuActionEvent;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
/**
 *
 * @author Ugo
 */
@ManagedBean
@SessionScoped
public class MenuBean implements Serializable{
    @EJB
    private UserManager userManager;
    
    @EJB
    private CalendarManager calendarManager;
     
    private DefaultMenuModel menuModel;
    private List<Users> users;
    private Calendar calendarLogged;
    private Calendar calendar;
     
    @PostConstruct
    public void init() {
        String visibility;
        calendarLogged = calendarManager.findCalendarByUser(userManager.getLoggedUser());
        if(calendarLogged.isPrivate() == true){
            visibility = "public";
        }
        else{
            visibility = "private";
        }
        menuModel = new DefaultMenuModel();
      
        
        DefaultSubMenu zeroSubmenu = new DefaultSubMenu("User");
        DefaultMenuItem item = new DefaultMenuItem("Logout");
        item.setIcon("ui-icon-power");
        item.setCommand("#{signinBean.logout()}");
        item.setAjax(false);
        zeroSubmenu.addElement(item);
        
        item = new DefaultMenuItem("Update profile");
        item.setIcon("ui-icon-contact");
        item.setHref("http://localhost:8080/trunk/user/profileUpdate.xhtml");
        item.setAjax(false);
        zeroSubmenu.addElement(item);
        
        item = new DefaultMenuItem("Notifications");
        item.setIcon("ui-icon-comment");
        item.setHref("http://localhost:8080/trunk/user/notification.xhtml");
        item.setId("notify");
        item.setAjax(false);
        zeroSubmenu.addElement(item);
        
        DefaultSubMenu firstSubmenu = new DefaultSubMenu("My calendar");
        item = new DefaultMenuItem("Explore");
        item.setIcon("ui-icon-star");
        item.setCommand("#{scheduleBean.onCalendarSelect}");
        item.setParam("userstring", "My calendar");
        item.setAjax(false);
        firstSubmenu.addElement(item);
        
        item = new DefaultMenuItem("Import");
        item.setIcon("ui-icon-arrowthickstop-1-s");
        item.setHref("http://localhost:8080/trunk/user/import.xhtml");
        item.setAjax(false);
        firstSubmenu.addElement(item);
        
        item = new DefaultMenuItem("Export");
        item.setAjax(false);
        item.setIcon("ui-icon-arrowthickstop-1-n");
        item.setHref("http://localhost:8080/trunk/user/export.xhtml");
        firstSubmenu.addElement(item);
        
        item = new DefaultMenuItem();
        item.setAjax(false);
        item.setIcon("ui-icon-transferthick-e-w");
        item.setCommand("#{menuBean.onVisibilitySelect}");
        item.setValue("Make it "+ visibility);
        firstSubmenu.addElement(item);  
        
        item = new DefaultMenuItem("New event");
        item.setAjax(false);
        item.setIcon("ui-icon-plusthick");
        item.setHref("http://localhost:8080/trunk/user/newEvent.xhtml");
        firstSubmenu.addElement(item);
        
        DefaultSubMenu secondSubmenu = new DefaultSubMenu("Public calendars");
        users = userManager.findOtherUsers();
        for(Users u: users){
            calendar = calendarManager.findCalendarByUser(u);
            if(calendar.isPrivate() == false){
                String email = u.getEmail();
                if(email.length()>20)
                    item = new DefaultMenuItem(email.substring(0, 21));
                else
                  item = new DefaultMenuItem(email);
                
                item.setIcon("ui-icon-calendar");
                item.setCommand("#{scheduleBean.onCalendarSelect}");
                item.setParam("userstring", u.getEmail());
                item.setAjax(false);
                secondSubmenu.addElement(item);
            }  
        } 
        menuModel.addElement(zeroSubmenu);
        menuModel.addElement(firstSubmenu);
        menuModel.addElement(secondSubmenu);
    }
    
    public DefaultMenuModel getModel() {
        return this.menuModel;
    }  
    
    public void onVisibilitySelect(MenuActionEvent event){
        DefaultMenuItem menuitem=(DefaultMenuItem)event.getMenuItem();
        if(menuitem.getValue().equals("Make it private"))
        {
            menuitem.setValue("Make it public");
            calendarLogged.setisPrivate(true);
            calendarManager.edit(calendarLogged);
        }
        else
        {
            menuitem.setValue("Make it private");
            calendarLogged.setisPrivate(false);
            calendarManager.edit(calendarLogged);
        }  
    }  
}